using System;
using System.Collections.Generic;
using System.Text;
using JobShopModel.Model.FactoryLvl;
using JobShopModel.Model.Transport;
using JobShopModel.Model.JobLvl;
using JobShopModel.Model.Station;
using JobShopModel.Model;

namespace JobShopModel.Events
{
    /// <summary>
    /// Set the State of the Transporter back to IDDLE when it is done transporting.
    /// </summary>
    public class EndTransport : TransportEvent
    {

        private Job currentJob;
        private StationsName destination;

        private StationsName Destination
        {
            get { return this.destination; }
        }

        /// <summary>
        /// Constructor
        /// </summary>
        /// <param name="sTime">Time of arrival</param>
        /// <param name="destination">Station the transporter is going to</param>
        /// <param name="currentJob">Job the transporter is transporting</param>
        public EndTransport(Time sTime, StationsName destination, Job currentJob)
            : base(sTime)
        {
            this.destination = destination;
            this.currentJob = currentJob;
        }

        /// <summary>
        /// Polymorphic method
        /// It will be instanced by all Events and excecuted by the eventloop
        /// </summary>
        public override void Process()
        {
            Factory fact = Factory.getInstance();
            Time elapsedTime = new Time();

            Logger.InsertElement(new LogItem(ItemPriorities.NORMAL, fact.CurrentTime, "Transport is arrived at "+Destination));

            //Add the Job to the Queue of the Station
            if (Destination != StationsName.IOSTATION)
            {
                RegularStation currentStation = (RegularStation)fact.Stations[Destination];

                //Add the job to the queue of the station
                currentStation.EnqueueJob(currentJob);
                //Increase jobcounter
               // currentStation.JobCounter++;

                //Store the start of waiting inside the queue for hte specific operation
                currentJob.CurrentOperation.setWaitingQueue(fact.CurrentTime);

                Logger.InsertElement(new LogItem(ItemPriorities.SLOW, fact.CurrentTime, "Transporter is adding "+currentJob+" to the queue of "+Destination));

                //"Tell" the station a new job/Operation is in its Queue
                PerformOperation nextEvent = new PerformOperation(fact.CurrentTime + elapsedTime, Destination);
                Factory.getInstance().EventQueue.insertEvent(nextEvent);
                Logger.InsertElement(new LogItem(ItemPriorities.CREATION, fact.CurrentTime, "The Transporter inserted: \"" + nextEvent.ToString() + "\""));
            }
            else
            {
                EndJob nextEvent = new EndJob(fact.CurrentTime + elapsedTime, currentJob);
                Factory.getInstance().EventQueue.insertEvent(nextEvent);
                Logger.InsertElement(new LogItem(ItemPriorities.CREATION, fact.CurrentTime, "The transporter inserted: \"" + nextEvent.ToString() + "\""));
            }
            

            //Set the transport back to IDDLE
            Transporter.StartIddle(fact.CurrentTime);
            //Increment its number of transportation
            Transporter.UpdateStats();

            //Call the transport Again to check if there is any new JOB in the FIFO while transporting....
            RequestTransport secondEvent = new RequestTransport(Factory.getInstance().CurrentTime);
            Factory.getInstance().EventQueue.insertEvent(secondEvent);
            Logger.InsertElement(new LogItem(ItemPriorities.CREATION, fact.CurrentTime, "The transporter inserted: \"" + secondEvent.ToString() + "\""));

        }

        public override string ToString()
        {
            return "End_Transport (" + ExecTime+")";
        }
    }
}
